# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
  _clangprefix=1
fi

_realname=lldb
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_pkgver=20.1.5
pkgver=${_pkgver/-/}
pkgrel=1
pkgdesc="Next generation, high-performance debugger (mingw-w64)"
url="https://lldb.llvm.org/"
msys2_references=(
  "cpe: cpe:/a:llvm:llvm"
)
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=("spdx:Apache-2.0 WITH LLVM-exception")
groups=($( (( _clangprefix )) && echo "${MINGW_PACKAGE_PREFIX}-toolchain"))
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-clang-libs"
         "${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-swig")
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra")
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${_pkgver}
_pkgfn=${_realname}-${_pkgver}.src
source=($_url/$_pkgfn.tar.xz{,.sig}
        ${_url}/cmake-${_pkgver}.src.tar.xz{,.sig})
sha256sums=('288ca08107746a050f6bed11bfcca642a15fdc1457d7b63a7c4d7d26ea506b44'
            'SKIP'
            '1b5abaa2686c6c0e1f394113d0b2e026ff3cb9e11b6a2294c4f3883f1b02c89c'
            'SKIP')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp2 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  mv cmake-${_pkgver}.src cmake
  cd "${srcdir}"/$_pkgfn
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} != i686 ]]; then
    _extra_config+=("-DLLDB_ENABLE_SWIG=ON")
  else
    _extra_config+=("-DLLDB_ENABLE_SWIG=OFF")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DLLVM_HOST_TRIPLE="${CARCH}-w64-windows-gnu" \
    -DLLVM_LINK_LLVM_DYLIB=ON \
    -DCLANG_LINK_CLANG_DYLIB=ON \
    -DLLDB_ENABLE_LIBEDIT=OFF \
    -DLLDB_EMBED_PYTHON_HOME=OFF \
    -DLLDB_INCLUDE_TESTS=OFF \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -Wno-dev \
    "${_extra_config[@]}" \
    ../$_pkgfn

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  # Compile Python scripts
   local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  ${MINGW_PREFIX}/bin/python -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/lldb"
  ${MINGW_PREFIX}/bin/python -O -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/lldb"

  install -Dm644 $_pkgfn/LICENSE.TXT "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
